using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using OneNote = Microsoft.Office.Interop.OneNote;

namespace Webber
{
    public static class ONXmlRequestor
    {
        /////////////////////////////
        // ONENOTE XML CONSTANTS
        /////////////////////////////
        private const string XML_NAMESPACE_ALIAS = "one";
        private const string XML_NAMESPACE_URL = "http://schemas.microsoft.com/office/onenote/2007/onenote";

        /////////////////////////////
        // CLASS MEMBERS
        /////////////////////////////
        private static string m_errorString = "";

        /////////////////////////////
        // CLASS METHODS
        /////////////////////////////
        
        private static void ClearError()
        {
            m_errorString = "";
        }

        private static void SetError(string msg)
        {
            m_errorString = msg;
        }

        // Requests XML from OneNIote
        // oneApp - OneNote reference
        // strParent - Parent node in OneNote (null for root)
        // enScope - OneNote Hierarchy scope
        // strXPath - xpath string to parse xml into a list
        // RETURNS:
        //     - null if there is an error
        //     - XmlNodeList of specified contents
        public static XmlNodeList GetNodes(OneNote.Application oneApp, string strParentId, OneNote.HierarchyScope enScope, string strXPath)
        {
            // Reset Error
            ClearError();

            // Call oneNote
            string strXmlNodeData = "";         // Raw XML Data
            if (oneApp == null)
            {
                SetError("Null reference to OneNote");
            }
            else
            {
                try
                {
                    oneApp.GetHierarchy(strParentId, enScope, out strXmlNodeData);
                }
                catch (Exception e)
                {
                    SetError(e.Message);
                }
            }
            
            // Aggregate xml data
            XmlNodeList nlResult = null;        // return value
            if (strXmlNodeData != "")
            {
                XmlDocument xdNodeDoc;              // Xml document
                XmlNamespaceManager nsmNodeNsmgr;   // Xml namespace manager
            
                // Setup xml doc of pages
                xdNodeDoc = new XmlDocument();
                xdNodeDoc.LoadXml(strXmlNodeData);

                // Setup XML name space
                nsmNodeNsmgr = new XmlNamespaceManager(xdNodeDoc.NameTable);
                nsmNodeNsmgr.AddNamespace(XML_NAMESPACE_ALIAS, XML_NAMESPACE_URL);
                
                // Query XML with XPath
                nlResult = xdNodeDoc.SelectNodes(strXPath, nsmNodeNsmgr);
            }

            return nlResult;
        }

        public static string GetLastError()
        {
            return m_errorString;
        }
    }
}
